CCP=@CXX@
CCC=@CC@
PROTOC=@protoc@

PROG=libp2vera@libext@
BUILD_DIR=@build_dir@

CFLAGS=@CFLAGS@ @includes@ -c 
LDFLAGS=-shared @LIBS@

SRCS=$(wildcard *.cpp)
OBJS:=$(SRCS:%.cpp=$(BUILD_DIR)/%.o)

PROTO=$(wildcard *.proto)
PROTO_CC=$(PROTO:%.proto=%.pb.cc)
PROTO_OBJ=$(PROTO_CC:%.cc=./obj/%.o)

all:$(BUILD_DIR)/$(PROG) @install_targets@ netfindtest

$(BUILD_DIR)/$(PROG): dirs $(PROTO_CC) $(PROTO_OBJ) $(OBJS)
	@echo [LD] $(PROG); \
	$(CCP) $(OBJS) $(PROTO_OBJ) $(LDFLAGS) -o $(BUILD_DIR)/$(PROG)

install.gnulinux: FORCE
	@cp $(BUILD_DIR)/$(PROG) ../bin/
	@cp $(BUILD_DIR)/$(PROG) ../../../bin/libs/
	@echo Done
	
install.macos: FORCE
	@install_name_tool -change /usr/local/lib/libprotobuf.7.dylib  @executable_path/libprotobuf.7.dylib $(BUILD_DIR)/$(PROG)
	@cp $(BUILD_DIR)/$(PROG) ../bin/
	@cp $(BUILD_DIR)/$(PROG) ../../../bin/libs/
	@echo Done

dirs: FORCE
	-@if [ ! -d $(BUILD_DIR) ]; then mkdir $(BUILD_DIR); fi
	-@if [ ! -d ../bin ]; then mkdir ../bin; fi
	-@if [ ! -d ../../../bin ]; then mkdir ../../../bin; fi
	-@if [ ! -d ../../../bin/libs ]; then mkdir ../../../bin/libs; fi

$(BUILD_DIR)/%.o: %.cpp
	@echo [CC] $< ; \
	$(CCP) $(CFLAGS) $(CFLAGS_AUX) -MD -o $@ -c $< ;

$(BUILD_DIR)/%.o: %.cc
	@echo [CC] $< ; \
	$(CCP) $(CFLAGS) $(CFLAGS_AUX) -MD -o $@ -c $< ;
	
%.pb.cc: %.proto
	@echo [PROTO] $< ; \
	$(PROTOC) -I=./ --cpp_out=./ $< ;

include $(wildcard $(BUILD_DIR)/*.d) 

netfindtest : FORCE
	@$(MAKE) -C ./tests/

clean:
	rm -rf $(BUILD_DIR) *.pb.cc *.pb.h
	@$(MAKE) clean -C ./tests/
	
FORCE:
